package com.lqr.wechat.activity;

import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.bridge.imconfig.NimTeamSDK;
import com.lqr.wechat.R;
import com.lqr.wechat.databinding.ActivityTeamNameSetBinding;
import com.lqr.wechat.utils.UIUtils;
import com.netease.nimlib.sdk.InvocationFuture;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.team.constant.TeamFieldEnum;
import com.netease.nimlib.sdk.team.model.Team;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * @创建者 CSDN_LQR
 * @描述 群名片界面
 */
public class TeamNameSetActivity extends BaseActivity {

    public static final String TEAM_ID = "teamId";
    private String mTeamId;
    private Team mTeam;
    private ActivityTeamNameSetBinding binding ;

    @Override
    protected int layoutId() {
        return R.layout.activity_team_name_set;
    }

    @Override
    public void initListener() {
        binding.lyToolbar.btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String teamName = binding.etName.getText().toString().trim();
                showWaitingDialog("修改群名片");
                Map<TeamFieldEnum, Serializable> fields = new HashMap<>(1);
                fields.put(TeamFieldEnum.Name, teamName);
                InvocationFuture<Void> invocationFuture = NimTeamSDK.updateTeamFields(mTeamId, fields);
                invocationFuture.setCallback(new RequestCallback<Void>() {
                    @Override
                    public void onSuccess(Void param) {
                        hideWaitingDialog();
                        finish();
                    }

                    @Override
                    public void onFailed(int code) {
                        UIUtils.showToast("修改群名片失败" + code);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onException(Throwable exception) {
                        exception.printStackTrace();
                        hideWaitingDialog();
                    }
                });
            }
        });
    }

    @Override
    public void init() {
        mTeamId = getIntent().getStringExtra(TEAM_ID);
        if (TextUtils.isEmpty(mTeamId)) {
            interrupt();
            return;
        }
        mTeam = NimTeamSDK.queryTeamBlock(mTeamId);
    }

    @Override
    public void initView() {
        binding = getBind() ;
        initToolbar();
        binding.etName.setText(mTeam.getName());
        binding.etName.setSelection(mTeam.getName().length());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    private void initToolbar() {
        setSupportActionBar(binding.lyToolbar.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("群名片");
        binding.lyToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);

        binding.lyToolbar.btnOk.setVisibility(View.VISIBLE);
        binding.lyToolbar.btnOk.setText("保存");
    }
}
